<template>
  <div @keyup.enter="getInvData()">
    <el-form class="query-table-form" :inline="true">
      <el-form-item>
        <el-input v-model="params.customer_name" placeholder="主体名称查询" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="params.corp_name" placeholder="负责人查询" :clearable="true"></el-input>
      </el-form-item>
      <el-form-item>
        <select-address
          v-model="params.address_code"
          placeholder="地址查询"
          clearable
          :auto-top="true"
          @change="getInvData()"
        ></select-address>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="getInvData()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          icon="el-icon-download"
          :loading="exportLoading"
          @click="exportExcel('项目调查（审批中）')"
        >
          导出
        </el-button>
      </el-form-item>
    </el-form>
    <c-table border v-loading="loading" :columns="tableColumns" :data="tableData">
      <template v-slot:address="scope">
        <div class="text-truncate" :title="scope.row.address">{{ scope.row.address }}</div>
      </template>
      <template v-slot:itemStatus="scope">
        <span class="status-label badge-warning">{{ scope.row.itemStatusLabel }}</span>
      </template>
      <template v-slot:name="scope">
        <el-popover placement="top-start" title="项目经理" trigger="hover">
          <div>
            <div>{{ `A角： ${scope.row.res_a_name || '-'}；` }}</div>
            <div class="mb-2">{{ `电话： ${scope.row.res_a_phone || '-'}` }}</div>
            <div>{{ `B角： ${scope.row.res_b_name || '-'}；` }}</div>
            <div>{{ `电话： ${scope.row.res_b_phone || '-'}` }}</div>
          </div>
          <div slot="reference">
            <div class="status-label badge-info text-left w-100">
              <div>A角:{{ scope.row.res_a_name }}</div>
              <div>B角:{{ scope.row.res_b_name }}</div>
            </div>
          </div>
        </el-popover>
      </template>
    </c-table>
    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="totalCount"
        @current-change="getInvData"
      ></el-pagination>
    </div>

    <el-dialog
      :visible.sync="reportVisible"
      :close-on-click-modal="false"
      append-to-body
      top="30px"
      width="1000px"
    >
      <el-tabs v-model="activeName">
        <el-tab-pane label="项目审批表" name="first">
          <approve-view :query-id="queryId" view-mode></approve-view>
        </el-tab-pane>
        <el-tab-pane label="担保意向函" name="letter">
          <letter-view :query-id="queryId"></letter-view>
        </el-tab-pane>
        <!-- <el-tab-pane label="立项附件">
          <item-attach :related-data="relatedData"></item-attach>
        </el-tab-pane> -->
        <el-tab-pane label="附件">
          <attach-view :queryId="queryId" :visible="reportVisible"></attach-view>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>
    <attach-cmpt :visible.sync="attachVisible" :customer-related-id="attachRelatedId"></attach-cmpt>

    <approve-dialog
      :visible.sync="editVisible"
      :query-id="editRow.customer_related_id"
      @success="reGetData"
    ></approve-dialog>

    <!-- <risk-report
      :visible.sync="riskVisible"
      :view-mode="false"
      :query-id="customerRelatedId"
    ></risk-report> -->
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { enumGuatype, enumProjectResStatus, enumProcessStatus } from '@/credit/plugins/enumTown'
import { baseTableColumns } from '@/credit/views/county/unionProvince/Approve'

import SelectAddress from '@/components/select/SelectAddress'
import AttachView from '@/credit/components/AttachView'
import AttachCmpt from '@/credit/views/city/program/Attach'

import taskTableMixin, { getFollow } from '@/credit/mixin/taskTableCountyUnionProvince'
import ApproveDialog from '@/credit/views/county/unionProvince/components/projectApprove/MainDialog'
import ApproveView from '@/credit/views/county/unionProvince/components/projectApprove/Main'
// import ItemAttach from '@/credit/views/county/unionProvince/components/ItemAttach'
// import { getStaff } from '@/common/api/auth'
// import RiskReport from '@/credit/views/county/unionProvince/components/riskReport/Dialog'
import LetterView from '@/credit/views/county/unionProvince/components/LetterView'
export default {
  components: {
    SelectAddress,
    AttachView,
    AttachCmpt,
    ApproveDialog,
    ApproveView,
    // ItemAttach,
    // RiskReport,
    LetterView,
  },
  mixins: [taskTableMixin],
  data() {
    return {
      enumGuatype,
      enumProjectResStatus,
      order: {
        approved_date: 'desc',
      },
      params: {
        index: 1,
        size: 15,
        address_code: '',
        customer_name: '',
        corp_name: '',
        // 'project_res_status.in': '1,2',
        // 'city_res_status_corp.lt': 2,
        'confuse_status.neq': 3,
        // 2-3
        // orWhereAndTemplate: 2,
        'project_res_status.eq': 1,
      },

      tableData: [],
      tableColumns: [
        ...baseTableColumns,
        { attrs: { label: '建议金额(万元)', prop: 'approved_funds', align: 'center' } },
        { attrs: { label: '审批开始时间', prop: 'res_date', width: 100 } },
        {
          attrs: { label: '合作银行', prop: 'bank_name', align: 'center' },
          process: v => v || '-',
        },
        { attrs: { label: '审批模式', prop: 'processLabel', width: 80, align: 'center' } },
        {
          attrs: { label: '待处理人员', prop: 'itemStatusLabel', align: 'center', width: 100 },
          slot: 'itemStatus',
        },
        { attrs: { label: '项目经理', prop: 'resName', width: 100 }, slot: 'name' },
        {
          attrs: { label: '操作', align: 'center', minWidth: 170 },
          operations: [
            {
              content: '项目审查',
              icon: 'fa fa-gavel',
              event: this.handleApprove,
              ifRender: row => row.invEditable,
            },
            // { content: '上传附件', icon: 'fa fa-clipboard', event: this.hanleAttach },
            // {
            //   content: '风险审查报告',
            //   icon: 'fa fa-file-o',
            //   event: this.handleRiskReport,
            //   ifRender: row => row.riskEditable,
            // },
            { content: '查看', icon: 'fa fa-search', event: this.handleView },
          ],
        },
      ],

      editRow: {},
      editVisible: false,
      reportVisible: false,
      relatedData: {},
      attachVisible: false,
      attachRelatedId: '',
      queryId: '',
      activeName: 'first',
      authorizedPerson: '',
      riskVisible: false,
      customerRelatedId: '',
    }
  },
  computed: {
    ...mapGetters(['job', 'user']),
  },
  watch: {},
  mounted() {},
  created() {
    this.params['service_id.eq'] = this.clientId
    this.getInvData()
  },
  methods: {
    async getInvData(index) {
      await this.getData(index)
      const record = await getFollow(this.tableData)
      this.tableData = this.tableData.map(it => {
        const data = record.find(item => item.wf_related_id === it.customer_related_id) || {}
        it.itemStatusLabel = data.innerFollow || ''
        it.invEditable = data.task_person?.includes(this.userId) ?? false
        return it
      })
    },
    // handleRiskReport(row) {
    //   this.customerRelatedId = row.customer_related_id
    //   this.riskVisible = true
    // },
    dataAdapter(item) {
      item.resName = `A角: ${item.res_a_name} B角: ${item.res_b_name}`
      item.guaType = enumGuatype[item.gua_type]
      item.res_date = this.$utils.dateFormat(item.res_date, this.dateFmt)
      item.processLabel = enumProcessStatus[+item.process_status] || ''
      return item
    },
    reGetData() {
      this.getInvData(this.params.index)
      this.$store.dispatch('getTaskCountTownProvince')
    },
    handleApprove(row) {
      this.editRow = { ...row }
      this.editVisible = true
    },
    hanleAttach(row) {
      this.attachVisible = true
      this.attachRelatedId = row.customer_related_id
    },
    handleView(row) {
      this.activeName = 'first'
      this.queryId = row.customer_related_id
      this.relatedData = row
      this.reportVisible = true
    },
  },
}
</script>
<style lang="scss" scoped>
.status-label {
  display: inline-block;
  padding: 0.25em 0.4em;
  font-size: 75%;
  font-weight: 700;
  line-height: 1;
  text-align: center;
  vertical-align: baseline;
  border-radius: 0.25rem;
  transition: color 0.15s;
}
</style>
